package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.CnocOsTablemappingConfig;
import com.soft.idc.common.util.JQPage;

public class TableMappingController extends Controller {
	public void index() {
		this.render("/perf_kpi_manager/tableMapping.html");
	}
	
	public void queryTableMapping() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		String table_name = this.getPara("table_name");
		StringBuffer sb = new StringBuffer();
		sb.append("select c.id, c.table_name, c.source_fileds, c.source_fileds_type, ");
		sb.append("c.dest_fileds, c.dest_fileds_type, c.state, c.create_date, c.seq, ");
		sb.append("case(c.state) when '10A' then '在用' when '10P' then '弃用' end as state_name");
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from cnoc_os_tablemapping_config c where 1=1 ");
		
		if(table_name !=null ){
			sqlFrom.append(" and c.table_name like '%" + table_name + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
		
	}
	
	public void add() {
		String table_name = this.getPara("table_name");
		String source_fileds = this.getPara("source_fileds");
		String source_fileds_type = this.getPara("source_fileds_type");
		String dest_fileds = this.getPara("dest_fileds");
		String dest_fileds_type = this.getPara("dest_fileds_type");
		String state = this.getPara("state");
		String seq = this.getPara("seq");
		Date now = new Date();
		CnocOsTablemappingConfig tableMapping = new CnocOsTablemappingConfig();
		tableMapping.setTableName(table_name);
		tableMapping.setSourceFileds(source_fileds);
		tableMapping.setSourceFiledsType(source_fileds_type);
		tableMapping.setDestFileds(dest_fileds);
		tableMapping.setDestFiledsType(dest_fileds_type);
		tableMapping.setSTATE(state);
		tableMapping.setSEQ(Integer.parseInt(seq));
		tableMapping.setCreateDate(now);
		boolean result = tableMapping.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String ids = this.getPara("ids");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] idArr = ids.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from cnoc_os_tablemapping_config where id in(");
				for(int i=0; i<idArr.length; i++){
					if(i==idArr.length-1){
						sb.append(idArr[i] + ")");
					}else{
						sb.append(idArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==idArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		String id = this.getPara("id");
		String table_name = this.getPara("table_name");
		String source_fileds = this.getPara("source_fileds");
		String source_fileds_type = this.getPara("source_fileds_type");
		String dest_fileds = this.getPara("dest_fileds");
		String dest_fileds_type = this.getPara("dest_fileds_type");
		String state = this.getPara("state");
		String seq = this.getPara("seq");
		CnocOsTablemappingConfig tableMapping = new CnocOsTablemappingConfig();
		tableMapping = tableMapping.findById(Integer.parseInt(id));
		tableMapping.setTableName(table_name);
		tableMapping.setSourceFileds(source_fileds);
		tableMapping.setSourceFiledsType(source_fileds_type);
		tableMapping.setDestFileds(dest_fileds);
		tableMapping.setDestFiledsType(dest_fileds_type);
		tableMapping.setSTATE(state);
		tableMapping.setSEQ(Integer.parseInt(seq));
		boolean result = tableMapping.update();
		this.renderJson(result);
	}
}
